package cn.exrick.xboot.flow.service.serviceimpl;

import cn.exrick.xboot.common.utils.BeanConvertUtils;
import cn.exrick.xboot.common.utils.SnowFlakeUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.exrick.xboot.flow.VO.FlowNodeinfoVo;
import cn.exrick.xboot.flow.entity.FlowNodeHandler;
import cn.exrick.xboot.flow.entity.FlowNodeinfo;
import cn.exrick.xboot.flow.mapper.FlowNodeHandlerMapper;
import cn.exrick.xboot.flow.mapper.FlowNodeinfoMapper;
import cn.exrick.xboot.flow.service.IFlowNodeinfoService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 流程节点表 服务实现类
 * </p>
 *
 * @author zy
 * @since 2022-08-31
 */
@Service
@Transactional
public class FlowNodeinfoServiceImpl extends ServiceImpl<FlowNodeinfoMapper, FlowNodeinfo> implements IFlowNodeinfoService {


    @Autowired
    private FlowNodeinfoMapper flowNodeinfoMapper;
    @Autowired
    private FlowNodeHandlerMapper flowNodeHandlerMapper;

    @Override
    public IPage<FlowNodeinfo> selectPage(Page<FlowNodeinfo> page, FlowNodeinfo flowNodeinfo) {
        QueryWrapper<FlowNodeinfo> query = Wrappers.query();
        QueryWrapper<FlowNodeinfo> entityWrapper = getEntityWrapper(query, flowNodeinfo);
        return flowNodeinfoMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<FlowNodeinfo> selectList(String templateId,String templateVersion,Integer setpId) {
        QueryWrapper<FlowNodeinfo> query = Wrappers.query();
        query.lambda().eq(FlowNodeinfo::getTemplateId,templateId)
                .eq(FlowNodeinfo::getTemplateVersion,templateVersion)
                .eq(setpId!=null,FlowNodeinfo::getSetpId,setpId);
        return flowNodeinfoMapper.selectList(query);
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<FlowNodeinfo> getEntityWrapper(QueryWrapper<FlowNodeinfo> entityWrapper, FlowNodeinfo flowNodeinfo) {

        return entityWrapper;
    }

    @Override
    public FlowNodeinfo getFlowNodeinfo(String templateId,String templateVersion, Integer setpId) {
        QueryWrapper<FlowNodeinfo> query = Wrappers.query();
        query.lambda().eq(FlowNodeinfo::getTemplateId,templateId)
                .eq(FlowNodeinfo::getTemplateVersion,templateVersion)
                .eq(FlowNodeinfo::getSetpId,setpId);
        return flowNodeinfoMapper.selectOne(query);
    }

    @Override
    public void addFlowNodeinfo(List<FlowNodeinfoVo> flowNodeinfoVos,String templateId,String templateVersion) {
        for (int i = 0; i < flowNodeinfoVos.size(); i++) {
            String id=String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId());
            FlowNodeinfoVo flowNodeinfoVo=flowNodeinfoVos.get(i);
            flowNodeinfoVo.setId(id);
            flowNodeinfoVo.setTemplateId(templateId);
            flowNodeinfoVo.setTemplateVersion(templateVersion);
            flowNodeinfoVo.setSetpId(i+1);

                if(i==flowNodeinfoVos.size()-1){
                    //结束节点
                    flowNodeinfoVo.setNodeType(1);
                }

            FlowNodeinfo flowNodeinfo= BeanConvertUtils.copy(flowNodeinfoVo,
                    FlowNodeinfo.class);
            flowNodeinfoMapper.insert(flowNodeinfo);
            //节点处理人
            List<FlowNodeHandler> flowNodeHandlers=flowNodeinfoVo.getFlowNodeHandlers();
            flowNodeHandlers.forEach(t->t.setFlowNodeinfoId(id));
            flowNodeHandlerMapper.insertBatch(flowNodeHandlers);
        }
    }



    @Override
    public List<FlowNodeinfoVo> getFlowNodeinfo(String templateId,String templateVersion) {
        QueryWrapper<FlowNodeinfo> query = Wrappers.query();
        query.lambda().eq(FlowNodeinfo::getTemplateId,templateId)
                .eq(FlowNodeinfo::getTemplateVersion,templateVersion)
        .eq(FlowNodeinfo::getDel,false);
        List<FlowNodeinfo> flowNodeinfos=flowNodeinfoMapper.selectList(query);
        if(flowNodeinfos.size()==0){
            return null;
        }
        List<FlowNodeinfoVo> flowNodeinfoVos=new ArrayList<>();
        flowNodeinfos.stream().forEach(t->{
            FlowNodeinfoVo flowNodeinfoVo=BeanConvertUtils.copy(t,FlowNodeinfoVo.class);
            QueryWrapper<FlowNodeHandler> queryFlowNodeHandler = Wrappers.query();
            queryFlowNodeHandler.lambda().eq(FlowNodeHandler::getFlowNodeinfoId,t.getId());
            List<FlowNodeHandler> flowNodeHandlers=
                    flowNodeHandlerMapper.selectList(queryFlowNodeHandler);
            flowNodeinfoVo.setFlowNodeHandlers(flowNodeHandlers);
            flowNodeinfoVos.add(flowNodeinfoVo);
        });
        return flowNodeinfoVos;
    }
    @Override
    public List<FlowNodeinfo> getFlowNodeInfo(String templateId,String templateVersion) {
        QueryWrapper<FlowNodeinfo> query = Wrappers.query();
        query.lambda().eq(FlowNodeinfo::getTemplateId,templateId)
                .eq(FlowNodeinfo::getTemplateVersion,templateVersion)
                .eq(FlowNodeinfo::getDel,false);
        List<FlowNodeinfo> flowNodeinfos=flowNodeinfoMapper.selectList(query);
        if(flowNodeinfos.size()==0){
            return null;
        }
        flowNodeinfos.remove(flowNodeinfos.size()-1);
        return flowNodeinfos;
    }
}
